package com.zzh.myapplication.datedialog;

import android.content.Context;
import android.content.res.Resources.NotFoundException;
import android.graphics.drawable.ColorDrawable;
import android.text.format.DateFormat;
import android.util.AttributeSet;
import android.widget.FrameLayout;
import android.widget.NumberPicker;
import android.widget.NumberPicker.OnValueChangeListener;

import net.icarplus.car.R;

import java.lang.reflect.Field;
import java.util.Calendar;
import java.util.Date;


public class DateTimePicker extends FrameLayout
{
	public DateTimePicker(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public DateTimePicker(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		init(context);
	}

	private NumberPicker mYearSpinner = null;
	private NumberPicker mMonthSpinner = null;
	private NumberPicker mDaySpinner = null;
	private NumberPicker mHourSpinner = null;
	private NumberPicker mMinuteSpinner = null;
	private Calendar mDate;
    private int mMonth,mDay,mYear,mHour,mMinute; 
    private String[] mDateDisplayValues = new String[7];
    private OnDateTimeChangedListener mOnDateTimeChangedListener;
    
    public DateTimePicker(Context context)
	{
    	super(context);
    	init(context);
	}
    void init(Context context){
//    	mDate = Calendar.getInstance();
//        mMonth=mDate.get(Calendar.HOUR_OF_DAY);
//        mDay=mDate.get(Calendar.MINUTE);
//   	 
//   	 inflate(context, R.layout.datedialog, this);
//   	 
//   	 mYearSpinner=(NumberPicker)this.findViewById(R.id.np_date);
//   	 mYearSpinner.setMinValue(0);
//     mYearSpinner.setMaxValue(6);
//     updateDateControl();
//   	 mYearSpinner.setOnValueChangedListener(mOnDateChangedListener);
//   	 
//   	 mMonthSpinner=(NumberPicker)this.findViewById(R.id.np_hour);
//   	 mMonthSpinner.setMaxValue(23);
//   	 mMonthSpinner.setMinValue(0);
//   	 mMonthSpinner.setValue(mMonth);
//   	 mMonthSpinner.setOnValueChangedListener(mOnHourChangedListener);
//   	 
//   	 mDaySpinner=(NumberPicker)this.findViewById(R.id.np_minute);
//   	 mDaySpinner.setMaxValue(59);
//   	 mDaySpinner.setMinValue(0);
//   	 mDaySpinner.setValue(mDay);
//   	 mDaySpinner.setOnValueChangedListener(mOnMinuteChangedListener);
    	
    	mDate = Calendar.getInstance();
    	mYear = mDate.get(Calendar.YEAR);
    	mMonth = mDate.get(Calendar.MONTH);
    	mDay = mDate.get(Calendar.DAY_OF_MONTH);
    	mHour = mDate.get(Calendar.HOUR_OF_DAY);
    	mMinute = mDate.get(Calendar.MINUTE);
    	Calendar cal = mDate;//Ϊ�˼���
    	cal.set(Calendar.DATE, 1);
    	cal.roll(Calendar.DATE, -1);
    	int maxDate = cal.get(Calendar.DATE);//�õ���������
    	 inflate(context, R.layout.datedialog, this);
    	 mYearSpinner=(NumberPicker)this.findViewById(R.id.np_year);
    	 mYearSpinner.setMinValue(1900);
         mYearSpinner.setMaxValue(2100);
         mYearSpinner.setValue(mYear);
    	 mYearSpinner.setOnValueChangedListener(mOnYearChangedListener);
    	 
    	 mMonthSpinner=(NumberPicker)this.findViewById(R.id.np_month);
    	 mMonthSpinner.setMaxValue(12);
    	 mMonthSpinner.setMinValue(1);
    	 mMonthSpinner.setValue(mMonth);
    	 mMonthSpinner.setOnValueChangedListener(mOnMonthChangedListener);
    	 
    	 mDaySpinner=(NumberPicker)this.findViewById(R.id.np_day);
    	 mDaySpinner.setMaxValue(maxDate);
    	 mDaySpinner.setMinValue(1);
    	 mDaySpinner.setValue(mDay);
    	 mDaySpinner.setOnValueChangedListener(mOnDayChangedListener);
    	 
    	 mHourSpinner=(NumberPicker)this.findViewById(R.id.np_hour);
    	 mHourSpinner.setMaxValue(23);
    	 mHourSpinner.setMinValue(0);
    	 mHourSpinner.setValue(mHour);
    	 mHourSpinner.setOnValueChangedListener(mOnHourChangedListener);
    	 mMinuteSpinner=(NumberPicker)this.findViewById(R.id.np_minute);
    	 mMinuteSpinner.setMaxValue(59);
    	 mMinuteSpinner.setMinValue(0);
    	 mMinuteSpinner.setValue(mMinute);
    	 mMinuteSpinner.setOnValueChangedListener(mOnMinuteChangedListener);
    	 setFgxColor();
    }
    
    private OnValueChangeListener mOnDateChangedListener=new OnValueChangeListener()
	{
		@Override
		public void onValueChange(NumberPicker picker, int oldVal, int newVal)
		{
			mDate.add(Calendar.DAY_OF_MONTH, newVal - oldVal);
			updateDateControl();
			onDateTimeChanged();
		}
	};
    
	private OnValueChangeListener mOnYearChangedListener=new OnValueChangeListener()
		{
			@Override
			public void onValueChange(NumberPicker picker, int oldVal, int newVal)
			{
				mYear=mYearSpinner.getValue();
				onDateTimeChanged();
			}
	};
    private OnValueChangeListener mOnMonthChangedListener =new OnValueChangeListener()
	{
		@Override
		public void onValueChange(NumberPicker picker, int oldVal, int newVal)
		{
			mMonth=mMonthSpinner.getValue();
			onDateTimeChanged();
		}
	};
	
	  private OnValueChangeListener mOnHourChangedListener=new OnValueChangeListener()
		{
			@Override
			public void onValueChange(NumberPicker picker, int oldVal, int newVal)
			{
				mHour=mHourSpinner.getValue();
				onDateTimeChanged();
			}
		};
	  private OnValueChangeListener mOnMinuteChangedListener=new OnValueChangeListener()
		{
			@Override
			public void onValueChange(NumberPicker picker, int oldVal, int newVal)
			{
				mMinute=mMinuteSpinner.getValue();
				onDateTimeChanged();
			}
		};
		
	private OnValueChangeListener mOnDayChangedListener=new OnValueChangeListener()
		{
			@Override
			public void onValueChange(NumberPicker picker, int oldVal, int newVal)
			{
				mDay=mDaySpinner.getValue();
				onDateTimeChanged();
			}
		};
	
	private void updateDateControl() 
    {
	 	Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(mDate.getTimeInMillis());
        cal.add(Calendar.DAY_OF_YEAR, -7 / 2 - 1);
        mYearSpinner.setDisplayedValues(null);
        for (int i = 0; i < 7; ++i) 
        {
            cal.add(Calendar.DAY_OF_YEAR, 1);
            mDateDisplayValues[i] = (String) DateFormat.format("MM.dd EEEE", cal);
        }
        mYearSpinner.setDisplayedValues(mDateDisplayValues);
        mYearSpinner.setValue(7 / 2);
        mYearSpinner.invalidate();
    }
	
	  public interface OnDateTimeChangedListener 
	  {
	        void onDateTimeChanged(DateTimePicker view, int year, int month, int day, int hour, int minute);
	  }
	
	  public void setOnDateTimeChangedListener(OnDateTimeChangedListener callback) 
	  {
	        mOnDateTimeChangedListener = callback;
	   }
	  
	  private void onDateTimeChanged() 
	  {
	        if (mOnDateTimeChangedListener != null)
	        {
	        	Calendar cal = Calendar.getInstance();
	        	cal.set(Calendar.YEAR, mYear);
	        	cal.set(Calendar.MONTH, (mMonth - 1));
	        	cal.set(Calendar.DAY_OF_MONTH, mDay);
	        	cal.set(Calendar.HOUR_OF_DAY, mHour);
	        	cal.set(Calendar.MINUTE, mMinute);
	        	cal.set(Calendar.SECOND, 0);
	        	cal.set(Calendar.DATE, 1);
	        	cal.roll(Calendar.DATE, -1);
	        	int maxDate = cal.get(Calendar.DATE);
	        	mDaySpinner.setMaxValue(maxDate);
	        	mDaySpinner.invalidate();
	        	//
	        	Calendar cal2 = mDate;
	        	cal.set(Calendar.DATE, 1);
	        	cal.roll(Calendar.DATE, -1);
	        	int maxDate2 = cal2.get(Calendar.DATE);
	        	if((maxDate2 > maxDate)&&(mDay>maxDate)){
	        		mDay = maxDate;
	        	}
	        	//
	            mOnDateTimeChangedListener.onDateTimeChanged(this, mYear,
	            		(mMonth - 1), mDay,mHour, mMinute);
	            
	        }
	    }
	  public void setDate(Date date){
		  mDate.setTime(date);
		  mYear = mDate.get(Calendar.YEAR);
	      mMonth = (mDate.get(Calendar.MONTH)+1);
	      mDay = mDate.get(Calendar.DAY_OF_MONTH);
	      mYearSpinner.setValue(mYear);
	      mMonthSpinner.setValue(mMonth);
	      mDaySpinner.setValue(mDay);
	      mHourSpinner.setValue(mHour);
	      mMinuteSpinner.setValue(mMinute);
		  mYearSpinner.invalidate();
		  mMonthSpinner.invalidate();
		  mDaySpinner.invalidate();
		  mHourSpinner.invalidate();
		  mMinuteSpinner.invalidate();
	  }
	  
	  private void setFgxColor(){//分割线
		  Field[] pickerFields = NumberPicker.class.getDeclaredFields();
	         for (Field pf : pickerFields) {
	             if (pf.getName().equals("mSelectionDivider")) {
	                 pf.setAccessible(true);
	                 try {
	                     pf.set(mYearSpinner, new ColorDrawable(getResources().getColor(R.color.app_color)));
	                     pf.set(mMonthSpinner, new ColorDrawable(getResources().getColor(R.color.app_color)));
	                     pf.set(mDaySpinner, new ColorDrawable(getResources().getColor(R.color.app_color)));
	                     pf.set(mHourSpinner, new ColorDrawable(getResources().getColor(R.color.app_color)));
	                     pf.set(mMinuteSpinner, new ColorDrawable(getResources().getColor(R.color.app_color)));
	                 } catch (IllegalArgumentException e) {
	                     e.printStackTrace();
	                 } catch (NotFoundException e) {
	                     e.printStackTrace();
	                 } catch (IllegalAccessException e) {
	                     e.printStackTrace();
	                 }
	                 break;
	             }
	         }
	  }
	  
}
